package com.ljp.chat.utils;

import cn.hutool.core.date.DateUtil;
import com.ljp.chat.exception.WarnException;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AccountUtils {

    /**
     * Get account string.
     * 获得账号
     *
     * @return the string
     */
    public static String getAccount() {
        String account;
        int targetNum = new Random().nextInt(100);

        if (3 > targetNum) {
            account = getAccountNum(6);
        } else if (5 > targetNum) {
            account = getAccountNum(7);
        } else if (8 > targetNum) {
            account = getAccountNum(8);
        } else {
            account = getAccountNum(9);
        }

        return account;
    }

    /**
     * Get account num string.
     * 根据参数获得对应位数的随机账号
     *
     * @param num the num   账号位数
     * @return the string
     */
    private static String getAccountNum(int num) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < num; i++) {
            int temp = new Random().nextInt(10);
            builder.append(temp);
        }
        return builder.toString();
    }


    /**
     * Is email string.
     * 利用正则表达式检查字符串是否为邮箱格式
     *
     * @param email the email
     * @return the string
     */
    public static boolean isEmail(String email) {
        String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return email.matches(regex);
    }

    /**
     * Get constellation string.
     * 根据日期获得星座
     *
     * @param dateStr the date str
     * @return the string
     */
    public static String getConstellation(String dateStr) throws WarnException {
        String pattern = "[1-2]\\d{3}-\\d{2}-\\d{2}";
        Pattern r = Pattern.compile(pattern);
        if (Pattern.matches(pattern, dateStr)) {
            Matcher m = r.matcher(dateStr);
            while (m.find()) {
                dateStr = m.group();
                return DateUtil.getZodiac(Integer.parseInt(dateStr.substring(5, 7)) - 1, Integer.parseInt(dateStr.substring(8)));
            }
        }
        throw new WarnException("Date Format error");
    }

    /**
     * 账号申请测试
     */
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10; i++) {
            System.out.println(getAccount());
        }
    }

}

